//
//  CHUtils.swift
//  yzb
//
//  Created by cyptt on 2021/12/6.
//

import UIKit
//import ObjectMapper

 public class CHUtils: NSObject {

    /**
     获取类名
     */
  public static func getClassName(_ aClass: AnyClass) -> String {
        
        let classN: String =  NSStringFromClass(aClass )
        
       let arr:[String] = classN.components(separatedBy: ".")
      
       let lastClassName:String = arr.last ?? "";
        return lastClassName
      
    }
    
    /*
     json文件转数组
     */
     public static func fileNameToArray(name:String) -> [Any]{
        guard let filePath:String = Bundle.main.path(forResource:name, ofType: "json")else{
            return [];
        }
    
        let url:URL = URL.init(fileURLWithPath: filePath)
        
        var data:Data
        do{
            data = try Data.init(contentsOf: url)
            let dataArr = try JSONSerialization.jsonObject(with: data, options: [])
    
            return dataArr as! [Any]
        } catch {
         
           
        }

        return []
    }
    
    /**
     获取类属性名字,可以获取带@objc标识的属性名字,因为是用的runtime,所以只能获取@objc的属性,swift特有的属性不能被获取
     @param clsType 类名字
     */
     public static func getClassPropertieNames(_ clsType:AnyClass){
        var prosCount:UInt32 = 0
        //获取属性列表,注意只有@objc的属性,才能被发现,因为class_copyPropertyList是oc的runtime方法
        let pros:UnsafeMutablePointer<objc_property_t>! =  class_copyPropertyList(clsType.self, &prosCount)
        let count:Int = Int(prosCount);
        //遍历属性名字
        for i in 0..<count {
            let pro: objc_property_t = pros[i]
            guard let proName:String? = String(cString: property_getName(pro))else{ return }
            print("属性名:",proName)
        }
        free(pros)
    }
  
}
